package vn.edu.uit.chemistry.utilities;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.util.Log;

public class ClientThreadReceiver implements Runnable {

	private Socket socket;
	private Thread thread;

	int fileSize = 30 * 1024;
	int bytesRead;
	int current = 0;
	String path = Environment.getExternalStorageDirectory().getAbsolutePath()
			.toString()
			+ "/hoa hoc";
	public static int percent = 0;
	String host = "";
	int total = 0;
	Context ctx;

	public ClientThreadReceiver(Socket _sk, String _host, Context _ctx)
			throws IOException {
		System.out.println("Client connected");
		this.socket = _sk;
		this.ctx = _ctx;
		this.host = _host;
	}

	public void start() {

		thread = new Thread(this);
		thread.start();
		while (!thread.isAlive()) {
			//
		}

	}

	public void stop() throws IOException {

		if (socket != null) {
			socket.shutdownInput();
			socket.shutdownOutput();
			socket.close();
		}
	}

	public void run() {
		try {
			receiveFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void receiveFile() throws IOException {

		SharedPreferences prefs = ctx.getSharedPreferences("FILENHAN", 0);
		String kind = prefs.getString("kind", "").toString().trim();
		String nameFile = prefs.getString("name", "").toString().trim();
		String sizeFile = prefs.getString("size", "").toString().trim();

		if (kind.equals("BKT15")) {
			path = path + "/kiem tra/kt15/";
		} else if (kind.equals("BKT45")) {
			path = path + "/kiem tra/kt45/";
		} else if (kind.equals("TLGV") || kind.equals("TLBB")) {
			path = path + "/tai lieu/";
		} else if (kind.equals("DAKT15")) {
			path = path + "/kiem tra/kt15/";
		} else if (kind.equals("DABCGV") || kind.equals("DABCBB")) {
			path = path + "/kiem tra/bai cu/";
		} else if (kind.equals("BKGV") || kind.equals("BKBB")) {
			path = path + "/bat ky/";
		}

		int total = Integer.parseInt(sizeFile);

//		if (total > fileSize) {
//			fileSize = 10 * 1024 * 1024;
//		} else {
//			fileSize = 30 * 1024;
//		}
		
		fileSize = total + 1024;
		
//		fileSize = total;
		
//		Log.d("total", ""+total);
//		Log.d("size", ""+fileSize);
		
		byte[] myByteArray = new byte[fileSize];
		InputStream is = socket.getInputStream();
		bytesRead = is.read(myByteArray, 0, myByteArray.length);
		current = bytesRead;

		do {
			bytesRead = is.read(myByteArray, current, myByteArray.length
					- current);
			Log.d("total", ""+bytesRead);
			if (bytesRead >= 0) {
				current += bytesRead;
				percent = current * 100 / total;

				prefs = ctx.getSharedPreferences("FILENHAN", 0);
				SharedPreferences.Editor editor = prefs.edit();
				editor.putInt("percent", percent);
				editor.commit();
			}

		} while (bytesRead > -1);
		
		prefs = ctx.getSharedPreferences("FILENHAN", 0);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putInt("percent", 100);
		editor.commit();

		FileOutputStream fos = new FileOutputStream(path + nameFile);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		bos.write(myByteArray, 0, current);
		bos.flush();
		bos.close();

		stop();
	}

}
